package com.drcoffee.drcoffeemall.controller;

import com.drcoffee.drcoffeemall.entry.Machine;
import com.drcoffee.drcoffeemall.entry.Video;
import com.drcoffee.drcoffeemall.service.MachineService;
import com.drcoffee.drcoffeemall.util.BodyToMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.crypto.Mac;
import javax.swing.text.EditorKit;
import java.io.IOException;
import java.util.Map;

/**
 * 机型
 *
 * @author ShiHao Zhou
 */
@RestController
@RequestMapping("/machine")
public class MachineController {

    @Autowired
    MachineService machineService;

    /*
     * 查询机器
     * */
    @GetMapping("/getMachine")
    public Map<String, Object> getMachine(@RequestParam Integer type_id, @RequestParam String search) {
//        System.out.println(machine.toString());
        return machineService.getMachine(type_id, search);
    }

    /*
     * 查询机器
     * */
    @GetMapping("/getMachineAll")
    public Map<String, Object> getMachineAll() {
        return machineService.getMachineAll();
    }


    /*
     * 根据id查询机器
     * */
    @GetMapping("/getMachineById")
    public Map<String, Object> getMachineById(@RequestParam Integer id) {
        return machineService.getMachineById(id);
    }


    @PostMapping("/insertData")
    public ResponseEntity<Object> insertData(@RequestParam("file") MultipartFile file, @RequestParam("file_name") String file_name, @RequestParam("machine") String machine) throws IOException {
        return machineService.insertData(file, file_name, BodyToMapUtil.convertJsonToMap(machine));
    }


    @GetMapping("/deleteOld")
    public Map<String, Object> deleteOld(@RequestParam Integer id) {
        return machineService.deleteOld(id);
    }

    @PostMapping("/updateMachine")
    public Map<String, Object> updateMachine(@RequestBody Machine machine) {
        return machineService.updateMachine(machine);
    }

    @PostMapping("/updateMachineText")
    public Map<String, Object> updateMachineText(@RequestBody Machine machine) {
        return machineService.updateMachineText(machine);
    }


}
